var SEPARATOR_COMMA=",";//默认分隔符
function stopContextMenu(){
    return false;
}
//document.oncontextmenu=stopContextMenu;
//function getContextPath(){
//    var pathName = document.location.pathname;
//    var index = pathName.substr(1).indexOf("/");
//    var result = pathName.substr(0,index+1);
//    return result;
//}
//json转换引号处理
function quoteReplace(str){
    if( str != null && typeof str != "undefined"){
        str = str.toString().replace(new RegExp('(["\"])', 'g'),"\\\"");
    }
    return str;
}
/*
 * 点击按钮后按钮变灰
 * btnID：按钮的id值
 * nums：变灰的时长（可选），默认两秒
 */
function btnControl(btnID,nums){
    var i=2;
    if(nums!="undefined" && nums>0){
        i=nums;
    }
    var btn=document.getElementById(btnID);
    var btnName=btn.value;
    btn.disabled=true;
    btn.value=btnName+i;
    var timer=setInterval(function(){
        btn.value=btnName+(parseInt(i)-1);
        i--;
        if(i<1){
            btn.disabled=false;
            i=0;
            btn.value=btnName;
            clearInterval(timer)
        }
    },1000)
}
//backspace按键处理
document.onkeydown = function(){
    if(event.keyCode == 8){//如果按下的是退格键
        var currElement=event.srcElement;
        if((currElement.tagName.toLowerCase()=="input")||(currElement.tagName.toLowerCase()== "textarea")){
            if(currElement.disabled || currElement.getAttribute('readonly')){
                event.returnValue = false;//不执行任何操作
            }
        }else{
            event.returnValue = false;//不执行任何操作
        }
    }
}

/**
 * js实现list
 *
 */
function List(){
    this.value = [];
    this.add = function(obj){
        return this.value.push(obj);
    };
    this.size = function(){
        return this.value.length;
    }
    this.get = function(index){
        return this.value[index];
    }
    this.remove = function(index){
        var array = new Array();
        for(var i=0;i<this.size();i++){
            if(i==index) continue;
            array.push(this.value[i]);
        }
        this.value=array;
        return array;
    }
    this.removeAll = function(){
        return this.value=[];
    }
    this.constains = function(obj){
        for(var i in this.value){
            if( obj == this.value[i] ){
                return true;
            }else{
                continue;
            }
        }
        return false;
    }
    this.toJson = function() {
        var s='[';
        for(var i=0;i<this.size();i++){
            var v=this.value[i];
            if(v!=null && v!="" && v.length>0 && v.substr(0,1)=="{"){
                s+=v;
            }else{
                s+='"'+quoteReplace(v)+'"';
            }
            if(i!=(this.size()-1)){
                s+=',';
            }
        }
        s+=']';
        return s;
    }
}

/**
 * js Map 实现
 * @returns {Map}
 */
function Map() {
    this.keys = [];
    this.data = {};
    this.put = function(key, value) {
        if (this.data[key] == null) {
            this.keys.push(key);
        }
        this.data[key] = value;
    };
    this.get = function(key) {
        return this.data[key];
    };
    this.contain = function(key) {
        var value = this.data[key];
        if (value)
            return true;
        else
            return false;
    };
    this.remove = function(key) {
        for(var index=0;index<this.keys.length;index++){
            if(this.keys[index]==key){
                this.keys.splice(index,1);
                break;
            }
        }
        this.data[key] = null;
    };
    this.each = function(fn) {
        if (typeof fn != 'function') {
            return;
        }
        var len = this.keys.length;
        for ( var i = 0; i < len; i++) {
            var k = this.keys[i];
            fn(k, this.data[k], i);
        }
    };
    this.entrys = function() {
        var len = this.keys.length;
        var entrys = new Array(len);
        for ( var i = 0; i < len; i++) {
            entrys[i] = {
                key : this.keys[i],
                value : this.data[i]
            };
        }
        return entrys;
    };
    this.isEmpty = function() {
        return this.keys.length == 0;
    };
    this.size = function() {
        return this.keys.length;
    };
    this.toString = function() {
        var s = "{";
        for ( var i = 0; i < this.keys.length; i++, s += ','){
            var k = this.keys[i];
            s += k + "=" + this.data[k];
        }
        s += "}";
        return s;
    };
    this.toJson = function() {
        var s = '{';
        for ( var i = 0; i < this.keys.length; i++){
            var k=quoteReplace(this.keys[i]);
            var v=quoteReplace(this.data[k]);
            s+='"'+k + '":"' + v+'"';
            if(i!=(this.keys.length-1)){
                s+=',';
            }
        }
        s += '}';
        return s;
    };
    this.toObj = function(){
        var jsonobj={};
        for ( var i = 0; i < this.keys.length; i++){
            var jk = this.keys[i];
            jsonobj[jk]=this.data[jk];
        }
        return jsonobj;
    }
}
var selRow;
function selectRowClk(clkObj){
    if(selRow){
        selRow.className = selRow.className.replace(new RegExp('(^|\\s)selectlight(\\s|$)'), ' ');
    }
    if (clkObj.className) {
        clkObj.className += ' selectlight';
    } else {
        clkObj.className = 'selectlight';
    }
    selRow=clkObj;
}
// 判断页面上checkbox选中的函数（flag：true允许多选，flase不允许多选）
// 单选返回checkbox的值，"-1"是没有选中  -2为多选
// 多选返回多个checkbox的值，"SEPARATOR_COMMA"号分隔。"-1"是没有选中
function fcheck(flag, objName){
    // 返回值
    var returnVal = "";
    // 获取所有选中的checkbox的对象数组
    var obj = document.getElementsByName(objName);
    for (var i = 0; i < obj.length; i ++){
        var checkBoxObj = obj[i];
        if (checkBoxObj) { // 对象有效
            // 判断对象是否为checkBox,并且是否选中
            if (checkBoxObj.type.toLowerCase() == "checkbox" && checkBoxObj.checked) {
                returnVal = returnVal + checkBoxObj.value + SEPARATOR_COMMA;
            }
        }
    }
    // 去除最后一个"SEPARATOR_COMMA"号
    if (returnVal.length > 0) {
        returnVal = returnVal.substring(0, returnVal.length-1);
    }
    // 没有选中的情况
    if(returnVal == ""){
        return -1;
    }
    if(!flag){
        if(returnVal.indexOf(SEPARATOR_COMMA)>0){ return -2; }
    }
    return returnVal;
}
function fradio(objName){
    var returnVal ="";
    var temp = document.getElementsByName(objName);
    for(var i=0;i<temp.length;i++)    {
        if(temp[i].checked){
            returnVal = temp[i].value;
            break;
        }
    }
    return returnVal;
}
/**
 * Ajax请求方法
 * @param url 提交的url
 * @param paras 参数，Map形式
 * @param callback 回调函数
 * @param isAsyn 可选参数，是否异步，不填为异步，true：异步；false：同步，
 */
function execAjax(resurl,paras,callback,isAsyn){
    if(isAsyn == null) isAsyn = true;
    var options={
        type:"post",
        async:isAsyn,
        dataType:"json",
        url:resurl,
        data:paras,
        success:function(response){
            if(callback && (callback instanceof Function)){
                callback(response);
            }
        }
    };
    $.ajax(options);
}
